/**
 * Copyright 2011, Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.codelab;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Entity;

/**
 * This servlet responds to the request corresponding to transacaos. The class
 * creates and manages the TransacaoEntity
 * 
 * @author
 */
@SuppressWarnings("serial")
public class UserStory13Servlet extends BaseServlet {

  private static final Logger logger = Logger.getLogger(UserStory13Servlet.class.getCanonicalName());

  /**
   * Searches for the entity based on the search criteria and returns result in
   * JSON format
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse resp)
	      throws ServletException, IOException {
	  	super.doGet(request,resp);
	    logger.log(Level.INFO, "Obtaining Transacao listing");
	    PrintWriter out = resp.getWriter();
	    
		String datainicial = request.getParameter("dataInicial");
		String dataFinal = request.getParameter("dataFinal");
		String pais = request.getParameter("pais");
		String estado = request.getParameter("estado");
		String cidade = request.getParameter("cidade");
		String tipoTransacao = request.getParameter("tipoTransacao");
		String tipoFraude = request.getParameter("tipoFraude");

	    Iterable<Entity> entitiesTransacao = Transacao.getTransacaoPeriodoTipo( Common.StrDateToDataScaf(datainicial) , Common.StrDateToDataScaf(dataFinal) , tipoTransacao);
	    Iterable<Entity> entitiesLocalidade = Localidade.getLocalidadePaisEstadoCidade(pais,estado,cidade);
	    Iterable<Entity> entitiesFraude = Fraude.getFraudePeriodoTipo(Common.StrDateToDataScaf(datainicial) , Common.StrDateToDataScaf(dataFinal) , tipoFraude);
	    out.println(Util.writeJSONTransacoesFraudulentas(entitiesFraude,entitiesTransacao,entitiesLocalidade));
	  }
  
}